//
//  TLChacheUtils.m
//  WXReader
//
//  Created by huaya on 2025/4/1.
//  Copyright © 2025 Andrew. All rights reserved.
//

#import "TLChacheUtils.h"
#import "TLFileUtils.h"
static const NSString * kMKCacheFilePlistSuffix = @"_mk.plist";


@implementation TLChacheUtils

#pragma mark - NSKeyedUnarchiver 归档
+ (void)saveByKeyedArchiverWith:(id)data fileName:(NSString *)fileName canClear:(BOOL)canClear{
    NSString *filePath = [self getFullPathByFileName:fileName isPlist:NO canClear:canClear];
    [TLFileUtils archiveObject:data toFile:filePath];
}

+ (id)getByKeyedUnarchiver:(NSString *)fileName withClass:(Class)clazz canClear:(BOOL)canClear{
    NSString *filePath = [self getFullPathByFileName:fileName isPlist:NO canClear:canClear];
    return [TLFileUtils unarchiverClass:clazz withFile:filePath];
}

+ (NSString *)getFullPathByFileName:(NSString *)fileName isPlist:(BOOL)isPlist canClear:(BOOL)canClear{
    NSString *mainPath = [TLFileUtils documentPath];
    NSString *folderPath;
    if (canClear) {
        folderPath = [mainPath stringByAppendingPathComponent:@"appData"];  //清理
    }else{
        folderPath = [mainPath stringByAppendingPathComponent:@"userData"]; //不清理
    }
    [TLFileUtils createDirAtPath:folderPath];
    
    NSString *filePath;
    if (isPlist) {
        filePath = [folderPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@%@",fileName,kMKCacheFilePlistSuffix]];
    }else{
        filePath = [folderPath stringByAppendingPathComponent:fileName];
    }
    return filePath;
}

+ (NSString *)getCanClearPath{
    NSString *path = [[TLFileUtils documentPath] stringByAppendingPathComponent:@"appData"];
    return path;
}
@end
